package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportPurchaseDocRpcService;
import com.hunttown.mes.rpc.domain.TransportPurchaseDocDTO;
import com.hunttown.mes.rpc.domain.query.TransportPurchaseDocDTOQuery;
import com.hunttown.mes.service.TransportPurchaseDocService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.TransportPurchaseDocConverts.convert;

/**
 * created by wangjunfu on 2022-01-28 11:17:15
 */
@Service
public class TransportPurchaseDocRpcServiceImpl implements TransportPurchaseDocRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportPurchaseDocRpcServiceImpl.class);

    private final TransportPurchaseDocService transportPurchaseDocService;

    @Autowired
    public TransportPurchaseDocRpcServiceImpl(TransportPurchaseDocService transportPurchaseDocService) {
        this.transportPurchaseDocService = transportPurchaseDocService;
    }

    @Override
    public TransportPurchaseDocDTO insert(TransportPurchaseDocDTO transportPurchaseDocDTO) {
        try {
            return convert(transportPurchaseDocService.insert(convert(transportPurchaseDocDTO)));
        } catch (Exception e) {
            logger.error("TransportPurchaseDoc create failed.", e);
            throw new RuntimeException("TransportPurchaseDoc create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportPurchaseDocDTO transportPurchaseDocDTO) {
        try {
            return transportPurchaseDocService.updateInfoById(convert(transportPurchaseDocDTO));
        } catch (Exception e) {
            logger.error("TransportPurchaseDoc update by id failed.", e);
            throw new RuntimeException("TransportPurchaseDoc update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportPurchaseDocDTOQuery transportPurchaseDocDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseDocDTOQuery.getW_id() != null && transportPurchaseDocDTOQuery.getW_id() > 0) {
                query.put("w_id", transportPurchaseDocDTOQuery.getW_id());
            }

            if (transportPurchaseDocDTOQuery.getIds() != null && !transportPurchaseDocDTOQuery.getIds().equals("")) {
                query.put("ids", transportPurchaseDocDTOQuery.getIds());
            }

            if (transportPurchaseDocDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseDocDTOQuery.getStopFlag());
            }

            if (transportPurchaseDocDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseDocDTOQuery.getDeleteFlag());
            }

            return transportPurchaseDocService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportPurchaseDoc update by query failed.", e);
            throw new RuntimeException("TransportPurchaseDoc update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseDocDTO selectById(Integer id) {
        try {
            return convert(transportPurchaseDocService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportPurchaseDoc select by id failed.", e);
            throw new RuntimeException("TransportPurchaseDoc select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportPurchaseDocDTO selectObjByQuery(TransportPurchaseDocDTOQuery transportPurchaseDocDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportPurchaseDocDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportPurchaseDocDTOQuery.getStopFlag());
            }

            if (transportPurchaseDocDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportPurchaseDocDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(transportPurchaseDocDTOQuery.getOrderBy())) {
                query.put("orderBy", transportPurchaseDocDTOQuery.getOrderBy());
            }

            return convert(transportPurchaseDocService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportPurchaseDoc select obj by query failed.", e);
            throw new RuntimeException("TransportPurchaseDoc select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportPurchaseDocDTO> getListForPage(TransportPurchaseDocDTOQuery transportPurchaseDocDTOQuery) {
        Query query = Query.page(transportPurchaseDocDTOQuery.getPn(), transportPurchaseDocDTOQuery.getPageSize());

        if (transportPurchaseDocDTOQuery.getId() != null && transportPurchaseDocDTOQuery.getId() > 0) {
            query.put("id", transportPurchaseDocDTOQuery.getId());
        }

        if (transportPurchaseDocDTOQuery.getIds() != null && !transportPurchaseDocDTOQuery.getIds().equals("")) {
            query.put("ids", transportPurchaseDocDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(transportPurchaseDocDTOQuery.getLikeName())) {
            query.put("likeName", transportPurchaseDocDTOQuery.getLikeName());
        }

        if (transportPurchaseDocDTOQuery.getPurchaseType() != null) {
            query.put("purchaseType", transportPurchaseDocDTOQuery.getPurchaseType());
        }

        if (transportPurchaseDocDTOQuery.getCompanyId() != null && transportPurchaseDocDTOQuery.getCompanyId() > 0) {
            query.put("companyId", transportPurchaseDocDTOQuery.getCompanyId());
        }

        if (transportPurchaseDocDTOQuery.getPurchaser() != null && transportPurchaseDocDTOQuery.getPurchaser() > 0) {
            query.put("purchaser", transportPurchaseDocDTOQuery.getPurchaser());
        }

        if (StringUtils.isNotBlank(transportPurchaseDocDTOQuery.getShowDate())) {
            query.put("showDate", transportPurchaseDocDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDocDTOQuery.getStartDate())) {
            query.put("startDate", transportPurchaseDocDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportPurchaseDocDTOQuery.getEndDate())) {
            query.put("endDate", transportPurchaseDocDTOQuery.getEndDate());
        }

        if (transportPurchaseDocDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportPurchaseDocDTOQuery.getStopFlag());
        }

        if (transportPurchaseDocDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportPurchaseDocDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(transportPurchaseDocDTOQuery.getOrderBy())) {
            query.put("orderBy", transportPurchaseDocDTOQuery.getOrderBy());
        }

        //区间日期
        if (StringUtils.isNotBlank(transportPurchaseDocDTOQuery.getDaterange())) {
            String[] date = transportPurchaseDocDTOQuery.getDaterange().split("~");
            if (date.length == 2) {
                query.put("startDate", date[0].trim());
                query.put("endDate", date[1].trim());
            }
        }

        List<TransportPurchaseDocDTO> list = convert(transportPurchaseDocService.selectByQuery(query));
        Page<TransportPurchaseDocDTO> page = Page.from(transportPurchaseDocDTOQuery.getPn(), transportPurchaseDocDTOQuery.getPageSize(), list);
        page.setTotalItems(transportPurchaseDocService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportPurchaseDocService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportPurchaseDoc delete failed.", e);
            throw new RuntimeException("TransportPurchaseDoc delete failed, Please order your code.", e);
        }
    }
}